#include "view_camera.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "common/common.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "font/font.h"
#include "font_mngr.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "math.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "view_camera.inc"
#include "motor.h"
#include "weather_ctrl.h"

void view_camera_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
    }
    draw_on_chip_gradients_image(6, 46, zip_image_circle, 0x67ff, 0x3cbf);
    draw_on_chip_gray_gif_array_filter_color(6, 46, zip_gif_camera, sizeof(zip_gif_camera) / sizeof(zip_gif_camera[0]), 0xFFFF);
}
void view_others_camera_static_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        bool connect = ble_if_connected();
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = 2000;
        if (!connect && disp_ctrl.com_data.i_temp[0] != connect)
        {
            motor_start(100);
            disp_ctrl.auto_refresh_count = 0;
        }
        disp_ctrl.com_data.i_temp[0] = connect;
        if (disp_ctrl.auto_refresh_count > 5)
        {
            display_show(view_camera, DISP_MODE_NORMAL);
            return;
        }
    }

    if (!disp_ctrl.com_data.i_temp[0])
    {
        display_show(view_camera, DISP_MODE_NORMAL);
        return;
    }
    // gui_fbuf_draw_user_gray_image_by_gradients(6, 46, 0x67ff, 0x3cbf, zip_image_circle);
    // draw_on_chip_gradients_image(6, 46, zip_image_circle, 0x67ff, 0x3cbf);

    // 画图标
    // gui_fbuf_draw_user_gray_image(6, 46, 0xFFFF, zip_gif_camera[index % 2]);
    draw_on_chip_image_ext(6, 46, zip_gif_camera[0], draw_type_filter_color);
}
void view_others_camera_shake_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        bool connect = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = 1000;
        if (!connect && disp_ctrl.com_data.i_temp[0] != connect)
        {
            motor_start(100);
            disp_ctrl.auto_refresh_count = 0;
            disp_ctrl.auto_refresh_time = 0;
        }
        disp_ctrl.com_data.i_temp[0] = connect;
    }

    if (!disp_ctrl.com_data.i_temp[0])
    {
        display_show(view_camera, DISP_MODE_NORMAL);
        return;
    }
    if (disp_ctrl.auto_refresh_count > 4)
    {
        display_show(view_others_camera_static, DISP_MODE_NORMAL);
        return;
    }
    draw_on_chip_gif_array_filter_color(6, 46, zip_gif_camera, sizeof(zip_gif_camera) / sizeof(zip_gif_camera[0]));
}
void view_camera_display(int view)
{
    switch (view)
    {
    case view_camera:
        view_camera_base_display();
        break;
    case view_others_camera_static:
        view_others_camera_static_display();
        break;
    case view_others_camera_shake:
        view_others_camera_shake_display();
        break;
    }
}

view_event_result_t view_others_camera_static_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
        display_show(DISP_TYPE_CAMERA, DISP_MODE_NORMAL);
        return view_event_result_handled;
#endif
    case event_view_exit:
        if (param->next_view != view_others_camera_shake)
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_LEVECAMERA);
        }
        return view_event_result_handled;
    case event_shake:
        if (ble_if_connected())
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_STARTPHOTO);
            motor_start_extend(MOTOR_VIBRATE_TYPE_SHARK);
            display_show(view_others_camera_shake, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}

view_event_result_t view_others_camera_shake_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
        display_show(DISP_TYPE_CAMERA, DISP_MODE_NORMAL);
        return view_event_result_handled;
#endif
    case event_view_exit:
        if (param->next_view != view_others_camera_static)
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_LEVECAMERA);
        }
        return view_event_result_handled;
    case event_shake:
        disp_ctrl.auto_refresh_count = 0;
        if (ble_if_connected())
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_STARTPHOTO);
            motor_start_extend(MOTOR_VIBRATE_TYPE_SHARK);
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_camera_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        if (ble_if_connected())
        {
            // display_show(view_prompt_camera_ready, DISP_MODE_NORMAL);
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_INTOCAMERA);
        }
        // else
        // {
        //     display_show(view_prompt_not_connect, DISP_MODE_NORMAL);
        // }
        return view_event_result_handled;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
        display_show(view_others_back - VIEW_LEVEL_1_SUB_COUNT, DISP_MODE_UP_MOVE);
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}
view_event_result_t view_camera_event(int view, event_param_t const *param)
{
    switch (view)
    {
    case view_camera:
        return view_others_camera_event(param);
    case view_others_camera_static:
        return view_others_camera_static_event(param);
    case view_others_camera_shake:
        return view_others_camera_shake_event(param);
    }
    return view_event_result_not_handled;
}
